<?php

namespace app\call\controller;

use app\admin\model\vip\Code;
use app\api\model\HotelOrder;
use app\api\model\Member;
use app\api\model\MemberVipCard;
use app\api\model\OrderLog;
use app\api\model\ScenicOrder;
use app\api\model\VipCard;
use app\api\model\VipOrder;
use app\api\model\VipUpgrade;
use app\BaseController;
use EasyWeChat\Factory;
use think\facade\Db;
use think\facade\Log;

class Pay
{
    protected $app;

    public function __construct()
    {
        $this->app = Factory::payment(config('wechat.wechat_pay'));
    }


    public function vipCardCallback()
    {
        $message = array(
            'out_trade_no' => 'VC24120857509910',
            'return_code' => "SUCCESS",
            "transaction_id" => "1111111"
        );
        $response = $this->app->handlePaidNotify(function ($message, $fail) {
        $order_id = $message['out_trade_no'];
        Log::write("支付回调" . json_encode($message));
        // 查询订单是否存在
        $order = VipOrder::where(['out_trade_no' => $order_id, 'status' => 0])->find();

        if (!$order) {
            return true;
        }
        //查询会员卡信息

        try {
            // 用户是否支付成功
            if ($message['return_code'] == 'SUCCESS') {
                $order->status = 1;
                $order->pay_time = time();
                $order->transaction_id = $message['transaction_id'];
                $order->save();
                return true;
            } else {
                    return $fail('支付失败，请稍后再通知我');
            }
        } catch (\Exception $e) {
            Db::rollback();
            Log::error('微信支付:' . $e->getMessage());
        }
        });

        $response->send();
    }



    public function vipUpgradeCallback()
    {

        $response = $this->app->handlePaidNotify(function ($message, $fail) {
            $order_id = $message['out_trade_no'];
            Log::write("支付回调" . json_encode($message));
            // 查询订单是否存在
            $order = VipUpgrade::where(['out_trade_no' => $order_id, 'status' => 0])->find();

            if (!$order) {
                return true;
            }
            //查询会员卡信息

            try {
                // 用户是否支付成功
                if ($message['return_code'] == 'SUCCESS') {

                    Db::startTrans();

                    $order->status = 1;
                    $order->pay_time = time();
                    $order->transaction_id = $message['transaction_id'];
                    $order->save();

                    $member_vip_card = (new MemberVipCard())->where(['member_id' => $order['member_id'],'vip_card_id' => $order['old_vip_card_id']])->find();
                    //执行更新用户会员卡
                    if($order['type'] == 1){
                        //续费
                        $vip_card = (new VipCard())->where(['id' => $order['old_vip_card_id']])->find();
                        if($vip_card['use_type'] == 1){
                            $update['nums'] = $member_vip_card['num'] + $vip_card['nums'];
                        }
                        $expire_date = date('Y-m-d H:i:s',$member_vip_card['expire_time']);
                        $update['expire_time'] = (new VipCard())->getExpireTime($expire_date,$vip_card['expire_type']);
                    }else{
                        //升级
                        $update['vip_card_id'] = $order['new_vip_card_id'];
                        //更新卡密绑定的会员卡
                        (new Code())->where(['number' => $member_vip_card['vip_code']])->update(['vip_card_id' => $order['new_vip_card_id']]);
                    }
                    (new MemberVipCard())->where(['member_id' => $order['member_id'],'vip_card_id' => $order['old_vip_card_id']])->update($update);

                    Db::commit();
                    return true;
                } else {
                    return $fail('支付失败，请稍后再通知我');
                }
            } catch (\Exception $e) {
                Db::rollback();
                Log::error('微信支付:' . $e->getMessage());
            }
        });

        $response->send();
    }




    public function scenicCallback()
    {
        $message = array(
            'out_trade_no' => 'VC24120597569949',
            'return_code' => "SUCCESS",
            "transaction_id" => "1111111"
        );
        $response = $this->app->handlePaidNotify(function ($message, $fail) {
            $order_id = $message['out_trade_no'];
            Log::write("支付回调" . json_encode($message));
            // 查询订单是否存在
            $order = ScenicOrder::where(['out_trade_no' => $order_id, 'status' => 0])->find();

            if (!$order) {
                return true;
            }
            //查询会员卡信息
            $vip_card = (new VipCard())->where(['id' => $order['vip_card_id']])->find();

            try {
                // 用户是否支付成功
                if ($message['return_code'] == 'SUCCESS') {
                    $order->status = 1;
                    $order->pay_time = time();
                    $order->transaction_id = $message['transaction_id'];
                    // 处理订单
                    Db::startTrans();
                    $order->save();
                    //执行扣次数
                    if($vip_card && $vip_card['use_type'] == 1){
                        (new MemberVipCard())->where(['member_id' =>$order['member_id'],'vip_card_id' => $vip_card['id'],'status'=>1])->setDec('nums');
                    }
                    (new OrderLog())->addLog(1,$order['id'],1,'预约已提交，正在处理中');
                    //添加金额
                    Db::commit();
                    return true;
                } else {
                    return $fail('支付失败，请稍后再通知我');
                }
            } catch (\Exception $e) {
                Db::rollback();
                Log::error('微信支付:' . $e->getMessage());
            }
        });

        $response->send();
    }



    public function hotelCallback()
    {
        $message = array(
            'out_trade_no' => 'HO24120699505354',
            'return_code' => "SUCCESS",
            "transaction_id" => "1111111"
        );
        $response = $this->app->handlePaidNotify(function ($message, $fail) {
            $order_id = $message['out_trade_no'];
            Log::write("支付回调" . json_encode($message));
            // 查询订单是否存在
            $order = HotelOrder::where(['out_trade_no' => $order_id, 'status' => 0])->find();

            if (!$order) {
                return true;
            }
            //查询会员卡信息
            $vip_card = (new VipCard())->where(['id' => $order['vip_card_id']])->find();

            try {
                // 用户是否支付成功
                if ($message['return_code'] == 'SUCCESS') {
                    $order->status = 1;
                    $order->pay_time = time();
                    $order->transaction_id = $message['transaction_id'];
                    // 处理订单
                    Db::startTrans();
                    $order->save();
                    //执行扣次数
                    if($vip_card && $vip_card['use_type'] == 1){
                        (new MemberVipCard())->where(['member_id' =>$order['member_id'],'vip_card_id' => $vip_card['id'],'status'=>1])->setDec('nums');
                    }
                    (new OrderLog())->addLog(2,$order['id'],1,'预约已提交，正在处理中');
                    //添加金额
                    Db::commit();
                    return true;
                } else {
                    return $fail('支付失败，请稍后再通知我');
                }
            } catch (\Exception $e) {
                Db::rollback();
                Log::error('微信支付:' . $e->getMessage());
            }
        });

        $response->send();
    }







}
